<!DOCTYPE html>
<html>
<head>
	<#include "/common/head.html"/>
	<script type="text/javascript" src="${base}/static/js/jquery.qrcode.min.js"></script>
</head>
<body>
	<div class="layui-fluid">
		<div class="layui-row layui-col-space15">
				<div class="layui-card">
					<div class="layui-card-body">
						<form class="layui-form layui-row layui-col-space16" lay-filter="table-form">
							<div class="layui-col-md2">
								<div class="layui-input-wrap">
									<div class="layui-input-prefix">
										<i class="layui-icon layui-icon-username"></i>
									</div>
									<input type="text" name="userName" placeholder="请输入账号"
										class="layui-input" lay-affix="clear">
								</div>
							</div>
							<div class="layui-col-md2 layui-btn-group">
								<button class="layui-btn" lay-filter="table-search" lay-submit>
									<i class="layui-icon layui-icon-search"></i> 搜索
								</button>
								<button type="reset" class="layui-btn layui-btn-primary">
									<i class="layui-icon layui-icon-clear"></i> 清除
								</button>
							</div>
						</form>

						<table class="layui-hide" id="table-main" lay-filter="table-main"></table>
						
						<script type="text/html" id="table-toolbar">
						<div class="layui-btn-group">
	<!--<button type="button" class="layui-btn layui-btn-blue" lay-event="upload">
    	<i class="layui-icon layui-icon-upload"></i> 导入
  	</button>-->
	<button type="button" class="layui-btn layui-btn-orange" lay-event="add">
    	<i class="layui-icon layui-icon-add-1"></i> 添加
  	</button>
	<button type="button" class="layui-btn layui-btn-danger" lay-event="del">
    	<i class="layui-icon layui-icon-delete"></i> 删除
  	</button>
</div>
</script>
						<script type="text/html" id="table-tool">
						<div class="layui-btn-group">
	<button type="button" class="layui-btn layui-btn-warm layui-btn-sm" lay-event="more">
    	更多 <i class="layui-icon layui-icon-more"></i>
  	</button>
</div>
</script>
					</div>
				</div>
		</div>
	</div>
	
	<script>
		layui.use(function() {
			var table = layui.table;
			var form = layui.form;
			var dropdown = layui.dropdown;
			var upload = layui.upload;

			table.render({
				custom:{
					editUri:'/v1/user'	
				},
				url : '/v1/user/page',
				where : {
				},
				cols : [ [ {
					type : 'checkbox',
					fixed : 'left'
				}, {
					field : 'id',
					title : 'ID',
					width : 180,
					fixed : 'left'
				}, {
					field : 'userName',
					title : '用户名',
					width : 150
				}, {
					field : 'name',
					title : '姓名',
					width : 150, 
					edit: 'text'
				}, {
					field : 'roles',
					title : '角色',
					width : 350, 
					templet: function(d){
						var h='';
						for(var r in d.roles){
							h+='<span class="layui-badge layui-bg-red">'+d.roles[r].name+'</span> ';
						}
						return h;
				    }
				}, {
					field : 'disable',
					title : '状态',
					width : 115,
					templet: function(d){
						if(d.disable){
							return '<span class="layui-badge layui-bg-red">禁用</span>';
						}else{
							return '<span class="layui-badge layui-bg-green">正常</span>';
						}
				    }
				}, {
					fixed : 'right',
					title : '操作',
					width : 125,
					minWidth : 125,
					toolbar : '#table-tool'
				} ] ]
			});
			
			table.on('toolbar(table-main)', function(obj) {
				if (obj.event === 'upload') {
					var uploadInst = upload.render({
					    elem: this,
					    accept:'file',
					    url: '/v1/user/import',
					    acceptMime:'*/*',
					    exts:'xls|xlsx',
					    done: function(res){
					    	alert('上传完成:'+res.code);
					    }
					 });
				}else if (obj.event === 'add') {
					xadmin.open('添加','/pp/system-user-edit-create')
				}else if (obj.event === 'del') {
					tableForDelete(table,obj,'/v1/user');
				}
			});
			
			table.on('tool(table-main)', function(obj){
				var data = obj.data;
				if(obj.event === 'more'){
					dropdown.render({
				        elem: this,
				        show: true,
				        data: [{
					      id: 'edit',
				          title: '信息编辑'
				        },{
					      id: 'restPwd',
				          title: '密码重置'
				        },{
					      id: 'secretKey',
				          title: '重置MFA'
				        }],
				        click: function(menudata){
				        	if(menudata.id === 'edit'){
					        	xadmin.open('编辑','/pp/system-user-edit-edit?id='+data.id)
					        } else if(menudata.id === 'restPwd'){
				        	 layer.confirm('确认重置密码吗？', function(index) {
			        			success=function(result){
			        				layer.alert("密码已重置为:123456", {
			        					icon : 6
			        				});
			        			}
			        			data={
			        				id : data.id,
			        				password : hexMD5("123456")
			        			}
			        			apiput("/v1/user",data,success);
			        		});
				          } else if(menudata.id === 'secretKey'){
				        	  layer.confirm('确认重置MFA吗？', function(index) {
				        			success=function(result){
				        				layer.close(index);
				        				layer.open({
				        				    type: 1,
				        				    title: '已重置MFA,请立即绑定',
				        				    closeBtn: 0,
				        				    area: ['auto', 'auto'],
				        				    shadeClose: true,
				        				    scrollbar:false,
				        				    content: '<div id="code" style="width:500px;height:500px;"></div>',
				        				    success:function(layero, index, that){
				        				    	$("#code").qrcode({
				        					        width: 500,
				        					        height: 500,
				        					        text: result.data,
				        					        correctLevel: 0
				        					    });
				        				    }
				        				  });
				        			}
				        			apiput("/v1/user/rest/mfa/"+data.id,{},success);
				        		});
				          }
			          },
			      })
				}
			});
		});
		
	</script>
	
</body>
</html>